package com.google.gwt.sample.dynatablerf.client.views;

import java.util.Set;

import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.sample.dynatablerf.client.presenters.MultipleSelectionDataPresenter.MultipleSelectionDataDisplay;
import com.google.gwt.sample.dynatablerf.client.theme.GrifoTheme;
import com.google.gwt.sample.dynatablerf.client.util.GrifoConstants;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoCheckBoxColumn;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoCheckBoxColumnHeader;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoSearchBox;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoSuggestOracle;
import com.google.gwt.sample.dynatablerf.client.widgets.HasGrifoSuggestHandler;
import com.google.gwt.sample.dynatablerf.client.widgets.HasMenuItens;
import com.google.gwt.sample.dynatablerf.shared.GrifoSharedConstants;
import com.google.gwt.sample.dynatablerf.shared.MaterialProjetoProxy;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent.HasSelectionChangedHandlers;

public class MaterialProjetoListView extends
		AbstractListView<MaterialProjetoProxy> implements
		MultipleSelectionDataDisplay<MaterialProjetoProxy> {

	private class MaterialDescricaoColumn extends
			Column<MaterialProjetoProxy, String> {
		public MaterialDescricaoColumn() {
			super(new TextCell());
		}

		@Override
		public String getValue(MaterialProjetoProxy object) {
			if (object.getMaterial() != null)
				return object.getMaterial().getDescricao();
			else
				return null;
		}
	}

	private class MaterialNumeroColumn extends
			Column<MaterialProjetoProxy, String> {
		public MaterialNumeroColumn() {
			super(new TextCell());
		}

		@Override
		public String getValue(MaterialProjetoProxy object) {
			if (object.getMaterial() != null)
				return object.getMaterial().getCodigoMaterial();
			else
				return null;
		}
	}

	private class MaterialQuantidadeColumn extends
			Column<MaterialProjetoProxy, Number> {

		public MaterialQuantidadeColumn() {
			super(new NumberCell());
		}

		@Override
		public Integer getValue(MaterialProjetoProxy object) {
			return object.getQuantidade();
		}
	}

	private class MaterialValorColumn extends
			Column<MaterialProjetoProxy, Number> {

		public MaterialValorColumn() {
			super(new NumberCell());
		}

		@Override
		public Double getValue(MaterialProjetoProxy object) {
			return object.getValor();
		}
	}

	private class TipoMaterialColumn extends
			Column<MaterialProjetoProxy, String> {

		public TipoMaterialColumn() {
			super(new TextCell());
		}

		@Override
		public String getValue(MaterialProjetoProxy object) {
			if (object.getMaterial() != null
					&& object.getMaterial().getTipoMaterial() != null)
				return object.getMaterial().getTipoMaterial().name();
			else
				return null;
		}
	}

	private DataGrid<MaterialProjetoProxy> materialDataGrid;
	private MultiSelectionModel<MaterialProjetoProxy> materialSelectionModel;
	private SimplePager materialPager;
	private Image closeTabImage;
	private GrifoSuggestOracle grifoSuggestOracle;
	private GrifoSearchBox grifoSearchBox;

	public MaterialProjetoListView() {
		FlowPanel mainpanel = new FlowPanel();
		initWidget(mainpanel);

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setWidth("100%");

		mainpanel.add(horizontalPanel);

		materialDataGrid = GrifoTheme.getGridPadrao(MaterialProjetoProxy.class);
		materialDataGrid.setPageSize(GrifoSharedConstants.GRID_NUM_ROWS);

		materialPager = new SimplePager();
		materialPager.setDisplay(materialDataGrid);

		grifoSuggestOracle = new GrifoSuggestOracle();
		grifoSearchBox = GrifoTheme.getSearchBox(grifoSuggestOracle);
		grifoSearchBox.setText(GrifoConstants.INSTANCE.pesquisaRapida());
		horizontalPanel.add(grifoSearchBox);

		horizontalPanel.add(materialPager);

		materialDataGrid.setSize("100%", "200px");
		mainpanel.add(materialDataGrid);

		materialSelectionModel = new MultiSelectionModel<MaterialProjetoProxy>();
		materialDataGrid.setSelectionModel(materialSelectionModel);

		GrifoCheckBoxColumnHeader<MaterialProjetoProxy> headerCheckMaterialColumn = new GrifoCheckBoxColumnHeader<MaterialProjetoProxy>(
				materialDataGrid, materialSelectionModel);
		GrifoCheckBoxColumn<MaterialProjetoProxy> checkMaterialColumn = new GrifoCheckBoxColumn<MaterialProjetoProxy>(
				materialSelectionModel);
		materialDataGrid.addColumn(checkMaterialColumn,
				headerCheckMaterialColumn);
		materialDataGrid.setSelectionModel(materialSelectionModel,
				DefaultSelectionEventManager
						.<MaterialProjetoProxy> createCheckboxManager());

		MaterialNumeroColumn materialNumeroColumn = new MaterialNumeroColumn();
		materialDataGrid.addColumn(materialNumeroColumn,
				GrifoConstants.INSTANCE.numero());

		MaterialDescricaoColumn materialDescricaoColumn = new MaterialDescricaoColumn();
		materialDataGrid.addColumn(materialDescricaoColumn,
				GrifoConstants.INSTANCE.descricao());

		MaterialQuantidadeColumn materialQuantidadeColumn = new MaterialQuantidadeColumn();
		materialDataGrid.addColumn(materialQuantidadeColumn,
				GrifoConstants.INSTANCE.quantidade());

		MaterialValorColumn materialValorColumn = new MaterialValorColumn();
		materialDataGrid.addColumn(materialValorColumn,
				GrifoConstants.INSTANCE.valor());

		TipoMaterialColumn tipoMaterialColumn = new TipoMaterialColumn();
		materialDataGrid.addColumn(tipoMaterialColumn,
				GrifoConstants.INSTANCE.tipoMaterial());

		closeTabImage = GrifoTheme.getCloseTabImage();
	}

	@Override
	public Widget getTabHeader() {
		Label label = GrifoTheme.getLabelPadrao();
		label.setText(GrifoConstants.INSTANCE.materiais());

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.add(label);
		horizontalPanel.add(closeTabImage);
		return horizontalPanel;
	}

	@Override
	public HasClickHandlers getCloseTabClickHandlers() {
		return closeTabImage;
	}

	@Override
	public HasSelectionHandlers<Suggestion> getHasSuggestionSelectionHandlers() {
		return grifoSearchBox;
	}

	@Override
	public HasGrifoSuggestHandler getGrifoSuggestHandler() {
		return grifoSuggestOracle;
	}

	@Override
	public HasSelectionChangedHandlers getSelectionChangedHandlers() {
		return materialSelectionModel;
	}

	@Override
	public HasData<MaterialProjetoProxy> getData() {
		return materialDataGrid;
	}

	@Override
	public Set<MaterialProjetoProxy> getSelectedSet() {
		return materialSelectionModel.getSelectedSet();
	}

	@Override
	public HasMenuItens getHasMenuItens() {
		return grifoSearchBox;
	}

}
